package com.gc.ykqx.ui.custom;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;

import com.gc.ykqx.R;
import com.gc.ykqx.service.entity.GridItem;
import com.gc.ykqx.ui.adapter.BaseGridViewAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ada on 2017/4/19.
 */

public class MenuView extends RelativeLayout implements RadioGroup.OnCheckedChangeListener, AdapterView.OnItemClickListener {

    private static final String TAG = "TabView";
    private Context mContext;
    private RadioGroup firstMenu;
    private GridView girdView;
    private OnFinishChooseListener listener;
    private BaseGridViewAdapter adapter;
    private List<GridItem> gridItemList;
    private int tabPosition;//菜单索引位置 0 雷达预测 1 雷达拼图 2金华雷达 3 台风路径
    private String[] gridNames;

    /*
    注意：一定调用addView 不然不显示
     */
    public MenuView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        View view = LayoutInflater.from(mContext).inflate(R.layout.menu_bar, null, false);
        addView(view);// TODO: 2017/7/10 important 不加不显示
        firstMenu = (RadioGroup) view.findViewById(R.id.first_menu);
        girdView = (GridView) view.findViewById(R.id.grid_view);
        init();
    }

    /*
    调用者设置的监听
     */
    public void setOnFinishChooseListener(OnFinishChooseListener listener) {
        this.listener = listener;
    }

    /*
     初始化
      */
    private void init() {
        gridItemList = new ArrayList<>();
        adapter = new BaseGridViewAdapter(mContext, gridItemList);
        girdView.setAdapter(adapter);
        gridNames = mContext.getResources().getStringArray(R.array.ycItem);
        //设置监听
        firstMenu.setOnCheckedChangeListener(MenuView.this);
        girdView.setOnItemClickListener(MenuView.this);
        setDefault();
    }

    /*
    一二级菜单监听事件
     */
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.rb_yc:
                tabPosition = 0;
                girdView.setVisibility(VISIBLE);
                gridNames = mContext.getResources().getStringArray(R.array.ycItem);
                break;
            case R.id.rb_zj:
                tabPosition = 1;
                girdView.setVisibility(VISIBLE);
                gridNames = mContext.getResources().getStringArray(R.array.zjItem);
                break;
            case R.id.rb_jh:
                tabPosition = 2;
                girdView.setVisibility(VISIBLE);
                gridNames = mContext.getResources().getStringArray(R.array.jhItem);
                break;
            case R.id.rb_tf:
                tabPosition = 3;
                girdView.setVisibility(GONE);
                break;
        }
        setDefault();
    }

    /*
      设置grid
    */
    private void setDefault() {
        if (gridItemList != null) {
            gridItemList.clear();
        }
        for (int i = 0; i < gridNames.length; i++) {
            GridItem gridItem = new GridItem();
            gridItem.setName(gridNames[i]);
            if (i == 0) {
                gridItem.setChecked(true);
            }
            gridItem.setChecked(false);
            gridItemList.add(gridItem);
        }
        changeItem(0);
    }

    /*

     */
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        changeItem(position);
    }

    private void changeItem(int position) {
        for (int i = 0; i < gridItemList.size(); i++) {
            GridItem gridItem = gridItemList.get(i);
            if (i == position) {
                gridItem.setChecked(true);
            } else {
                gridItem.setChecked(false);
            }
        }
        adapter.notifyDataSetChanged();
        if (listener != null) {
            String id = tabPosition + "" + position;
            listener.onFinishChosen(id);
        }
    }

    /*
    自定义接口,需调用者实现 获取选择内容
     */
    public interface OnFinishChooseListener {
        void onFinishChosen(String id);
    }
}
